package com.qf.market.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.market.po.Customer;
import com.qf.market.service.CustomerService;
import com.qf.market.utils.LayUIOperate;
import com.qf.market.utils.TableData;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("/getAllCustomerList")
    @ResponseBody
    public TableData getAllCustomerList(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String name = request.getParameter("name");
        String company = request.getParameter("company");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String status = request.getParameter("status");
        String divide = request.getParameter("divide");
        Map params = new HashMap();
        params.put("name", name);
        params.put("company", company);
        params.put("province", province);
        params.put("city", city);
        params.put("status", status);
        params.put("divide", divide);

        if("0".equals(province)){
            params.put("province", null);
        }else {
            params.put("province", province);
        }
        if("0".equals(city)){
            params.put("city", null);
        }else {
            params.put("city", city);
        }

        System.out.println(params);
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Customer> data = customerService.getAllCustomerByPage(params);
        System.out.println(data);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        System.out.println(params);
        return tableData;
    }
    @RequestMapping("/addCustomer")
    @ResponseBody
    public LayUIOperate addCustomer(@RequestBody Customer customer){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("addCustomer -->"+customer);
        boolean f= customerService.addCustomer(customer);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户添加成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户添加失败");
        }
        return operate;
    }
    @RequestMapping("/updateCustomer")
    @ResponseBody
    public LayUIOperate updateCustomer(@RequestBody Customer customer){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("updateCustomer -->"+customer);
        boolean f= customerService.updateCustomer(customer);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户修改成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户修改失败");
        }
        return operate;
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public LayUIOperate updateStatus(@RequestBody Customer customer){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("updateStatus -->"+customer);
        if("1".equals(customer.getStatus())){
            customer.setStatus("0");
        }else{
            customer.setStatus("1");
        }
        System.out.println("updateStatus -->"+customer);
        boolean f= customerService.updateStatus(customer);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("状态修改成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("状态修改失败");
        }
        return operate;
    }
}
